/**
 * Created by fupeng on 17/3/6.
 */
// var dao = require('../dao/CommentDao');
var async = require('async');
var tool = require('../../common/utils/appUtils');
var moment = require('moment');

module.exports = {
    postCommentListNew: function (params, callBack) {
        let page = params[1]

        /*  查询出replyname的sql
         select cm.*,n.nickname as replyname from (SELECT
         tt.*, muu.*
         FROM
         (
         SELECT
         t.*, amca.cid AS agreed
         FROM
         (
         SELECT
         mc.*, COUNT(mca.cid) AS sums
         FROM
         movie_comment AS mc
         LEFT JOIN movie_comment_agree AS mca ON mca.cid = mc.id
         WHERE
         mc.mid = 'BESTV170215142531005959'
         GROUP BY
         mc.id DESC
         -- LIMIT 10
         ) AS t
         LEFT JOIN movie_comment_agree AS amca ON t.id = amca.cid
         AND openid = '0000'
         GROUP BY
         id DESC
         ) AS tt
         LEFT JOIN member_user AS muu ON muu.openid = tt.open_id
         ORDER BY
         id DESC) as cm LEFT JOIN member_user as n ON n.openid=(select mm.open_id from movie_comment as mm where mm.id= cm.cm_id)
         */

        let sql = 'SELECT tt.*, muu.*  FROM  (  SELECT  t.*, amca.cid AS agreed  FROM' +
            ' (  SELECT  mc.*, COUNT(mca.cid) AS sums  FROM  movie_comment AS mc  LEFT JOIN movie_comment_agree AS mca ON mca.cid = mc.id' +
            ' WHERE  mc.mid =? GROUP BY mc.id DESC LIMIT 10  ) AS t  LEFT JOIN movie_comment_agree AS amca ON t.id = amca.cid  AND openid =? GROUP BY  id DESC ) AS tt LEFT JOIN member_user AS muu ON muu.openid = tt.open_id  ORDER BY id DESC'

        tool.baseDao('', sql, [params[0], params[2]], function (result) {
            if (!result.isSuccess) {
                callBack({
                    isSuccess: false
                })
            }
            for (var i = 0; i < result.rows.length; i++) {
                result.rows[i].create_time = moment(result.rows[i].create_time).format('YY-MM-DD').toString();
            }
            callBack({
                isSuccess: true,
                data: result
            })
        })
    },
    /**
     * ajax 添加评论
     * @param params
     * @param callback
     */
    postaddcomment: function (params, callback) {
        tool.baseDao('postaddcomment', 'insert into movie_comment (mid,open_id,comment,score,create_time)values(?,?,?,?,now()) ', params, function (reslut) {
            if (reslut.isSuccess) {
                if (reslut.rows.affectedRows == 1) {
                    callback({isSuccess: true});
                } else {
                    callback({isSuccess: false});
                }
            } else {
                callback({isSuccess: false});
            }
        })
    },
    /**
     * ajax 添加回复
     * @param params
     * @param callback
     */
    postreplycomment: function (params, callback) {
        tool.baseDao('postaddcomment', 'insert into movie_comment (mid,open_id,comment,cm_id,create_time)values(?,?,?,?,now()) ', params, function (reslut) {
            if (reslut.isSuccess) {
                if (reslut.rows.affectedRows == 1) {
                    callback({isSuccess: true});
                } else {
                    callback({isSuccess: false});
                }
            } else {
                callback({isSuccess: false});
            }
        })
    },


    postCommentList: function (params, callBack) {

        let page = params[1]

        let sql = 'SELECT tt.*, muu.*  FROM  (  SELECT  t.*, amca.cid AS agreed  FROM' +
            ' (  SELECT  mc.*, COUNT(mca.cid) AS sums  FROM  movie_comment AS mc  LEFT JOIN movie_comment_agree AS mca ON mca.cid = mc.id' +
            ' WHERE  mc.mid =? GROUP BY mc.id DESC LIMIT ?,10  ) AS t  LEFT JOIN movie_comment_agree AS amca ON t.id = amca.cid  AND openid =? GROUP BY  id DESC ) AS tt LEFT JOIN member_user AS muu ON muu.openid = tt.open_id ORDER BY id DESC'

        tool.baseDao('', sql, [params[0], page,params[2]], function (result) {
            if (!result.isSuccess) {
                callBack({
                    isSuccess: false
                })
            }
            for (var i = 0; i < result.rows.length; i++) {
                result.rows[i].create_time = moment(result.rows[i].create_time).format('YY-MM-DD').toString()
            }
            callBack({
                isSuccess: true,
                data: result
            })
        })


        // async.waterfall(
        //     [
        //         function (callback) {
        //             var sql = 'SELECT t.*,amca.cid as agreed FROM (SELECT mc.*, COUNT(mca.cid) AS sums FROM movie_comment AS mc LEFT JOIN movie_comment_agree AS mca ' +
        //                 'ON mca.cid = mc.id   WHERE mc.mid = ? GROUP BY mc.id DESC LIMIT ?,10) as t LEFT JOIN movie_comment_agree AS amca  ' +
        //                 ' ON t.id=amca.cid and openid=?  GROUP BY id desc';
        //             // tool.baseDao('', 'select * from movie_comment where mid=? order by id desc limit ?,10 ', [params[0], page], function (result) {
        //             tool.baseDao('', sql, [params[0], page, params[2]], function (result) {
        //                 if (!result.isSuccess) {
        //                     return
        //                 }
        //                 for (var i = 0; i < result.rows.length; i++) {
        //                     result.rows[i].create_time = moment(result.rows[i].create_time).format('YY-MM-DD').toString()
        //                 }
        //                 callback(null, result);
        //             });
        //         },
        //         function (arg, callback) {
        //             if (arg.rows.length < 0) {
        //                 return
        //             }
        //             for (let j = 0; j < arg.rows.length; j++) {
        //                 tool.baseDao('', 'select * from member_user where openid=?  ', arg.rows[j].open_id, function (result) {
        //                     if (!result.isSuccess) {
        //                         return callback()
        //                     }
        //                     arg.rows[j].user = result.rows[0];
        //                     if (j == arg.rows.length - 1) {
        //                         callback(null, arg);
        //                     }
        //                 });
        //             }
        //         }
        //     ],
        //     function (err, result) {
        //         if (err) {
        //             return
        //         }
        //         callBack({
        //             isSuccess: true,
        //             data: result
        //         })
        //     }
        // )

    },
    postComment: function (params, callback) {
        tool.baseDao('', 'insert into movie_comment (open_id,comment,mid,create_time,score) values(?,?,?,?,?)', params, function (result) {
            if (!result.isSuccess) {
                return
            }
            callback(result);
        });
    },
    getMoreComment: function (params, callback) {

    },
    postCommentAgree: function (params, callback) {
        tool.baseDao('postCommentAgree', 'select * from movie_comment_agree where cid=? and openid=?', [params[0], params[1]], function (result) {
            if (result.isSuccess && result.rows.length > 0) {
                //有点赞，更新
                if (params[2]) {
                    tool.baseDao('', 'insert into movie_comment_agree (cid,openid) values(?,?)', [params[0], params[1]], function (res) {
                        if (res.isSuccess && res.rows.affectedRows == 1) {
                            callback({isSuccess: true});
                            return
                        } else {
                            callback({isSuccess: false});
                            return
                        }
                    })
                } else {
                    tool.baseDao('', 'delete from movie_comment_agree where cid=? and openid=?', [params[0], params[1]], function (res) {
                        if (res.isSuccess && res.rows.affectedRows == 1) {
                            callback({isSuccess: true});
                            return
                        } else {
                            callback({isSuccess: false});
                            return
                        }
                    })
                }

                // tool.baseDao('', 'update movie_comment_agree set agree=? where cid=? and openid=?', [params[2], params[0], params[1]], function (res) {
                //     if (res.isSuccess && res.rows.affectedRows == 1) {
                //         callback({isSuccess: true});
                //         return
                //     } else {
                //         callback({isSuccess: false});
                //         return
                //     }
                // })
            } else if (result.rows.length == 0) {
                //没有评论，插入记录
                tool.baseDao('', 'insert into movie_comment_agree (cid,openid) values(?,?)', params, function (res) {
                    if (res.isSuccess && res.rows.affectedRows == 1) {
                        callback({isSuccess: true});
                        return
                    } else {
                        callback({isSuccess: false});
                        return
                    }
                })
            } else {
                callback({isSuccess: false})
                return;
            }
        });
    }
}